package com.harvest.scientific.ladder.util

import android.content.Context
import android.content.SharedPreferences


object SpUtil {

    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var editor: SharedPreferences.Editor

    const val KYE_USER_ID = "freezingSaleswomanFurnishedDuckling"
    const val KYE_USER_TOKE = "naturalPardonSmallBuddhism"
    const val KYE_USER_PHONE = "uselessElectricAnimal"
    const val KYE_USER_TEST_CUST_FLAG = "testCustFlag"
    const val KYE_USER_NEW_CUST_FLAG = "newCustFlag"
    const val KEY_IS_ZH = "is_zh_locale"
    const val KYE_USER_NAME_ONE = "festivalPaper"
    const val KYE_USER_NAME_TWO = "suddenGasTwin"

    fun init(context: Context) {
        sharedPreferences = context.getSharedPreferences(
            "FancyArgent",
            Context.MODE_PRIVATE
        )
        editor = sharedPreferences.edit()
    }

    fun put(key: String, data: Any) {
        when(data) {
            is String -> {
                editor.putString(key, data)
            }
            is Int -> {
                editor.putInt(key, data)
            }
            is Boolean -> {
                editor.putBoolean(key, data)
            }
            is Float -> {
                editor.putFloat(key, data)
            }
            is Long -> {
                editor.putLong(key, data)
            }
            else -> {
                editor.putString(key, data.toString())
            }
        }
        editor.commit();
    }

    fun getInt(key: String, defaultData: Int?): Int  = sharedPreferences.getInt(key,defaultData ?: 0)

    fun getString(key: String, defaultData: String): String  = sharedPreferences.getString(key,defaultData) ?: ""

    fun getBoolean(key: String, defaultData: Boolean?): Boolean  = sharedPreferences.getBoolean(key,defaultData ?: false)

    fun getFloat(key: String, defaultData: Float?): Float  = sharedPreferences.getFloat(key,defaultData ?: 0f)

    fun getLong(key: String, defaultData: Long?): Long  = sharedPreferences.getLong(key,defaultData ?: 0)

    fun isLogin(): Boolean {
        return getString(KYE_USER_ID,"").isNotEmpty()
    }
    fun getUserId() = getString(KYE_USER_ID, "")

    fun isTestCust() = getString(KYE_USER_TEST_CUST_FLAG, "0") == "1"

}

fun isLogin() = SpUtil.isLogin()